package cn.sjxy.regi.controller;

import cn.sjxy.regi.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

/**
 * ClassName:CommonController
 * Package:cn.sjxy.regi.controller
 * Description:
 *
 * @Date:2022/6/1 16:00
 * @Author:SXW
 */
@RequestMapping("/common")
@RestController
@Slf4j
public class CommonController {

    // 路径是动态获取的，修改配置文件即可修改存储位置
    @Value("${regi.path}/img/")
    private String imgPath;


    /**
     * @param file 参数名，必须要与上传时输入框的名字保持一致
     * @return
     */
    @PostMapping("/upload")
    public R<String> upload(MultipartFile file) {

        // 判断是否存在目录，不存在则先创建
        File dir = new File(imgPath);
        if (!dir.exists()) {
            // 创建目录
            dir.mkdir();
        }
        // 上传成功的文件是存放在服务器的临时目录的，需要转存到指定位置，否则请求完成后临时文件将被删除
        String originalFilename = file.getOriginalFilename();
        // 以最后一个 “.” 作为切割条件，获取上传时文件的后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 文件名使用 uuid 随机生成，防止文件名重复造成覆盖
        String fileName = UUID.randomUUID().toString() + suffix;

        try {
            file.transferTo(new File(imgPath + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.success(fileName);
    }


    @GetMapping("/download")
    public void download(String name, HttpServletResponse response) {
        FileInputStream fileInputStream = null;
        ServletOutputStream outputStream = null;
        try {
            // 输入流，读取文件内容
            fileInputStream = new FileInputStream(new File(imgPath + "/" + name));
            // 输出流，输出到浏览器
            outputStream = response.getOutputStream();

            response.setContentType("img/jpeg");

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
                outputStream.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
